package me.helllp.bootman.back.logic.system.services.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.helllp.bootman.back.logic.system.vo.User001Input;
import me.helllp.bootman.back.mapper.SusrPermissMapper;
import me.helllp.bootman.back.mapper.SusrRoleMapper;
import me.helllp.bootman.back.mapper.SusrRolePermissMapper;
import me.helllp.bootman.back.mapper.SusrUserMapper;
import me.helllp.bootman.back.mapper.SusrUserRoleMapper;
import me.helllp.bootman.back.model.SusrUser;
import me.helllp.bootman.back.utils.LocalSessionFactory;
import me.helllp.bootman.common.services.IdService;
import me.helllp.bootman.common.services.LoginUserServices;
import me.helllp.bootman.common.utils.PasswordHelper;

/**
 * bootman back的用户权限服务
 * 
 * @author Administrator
 *
 */
@Service
public class BackUserService implements LoginUserServices<User001Input>{
	@Autowired
	private SusrUserMapper userMapper;
	
	@Autowired
	private SusrPermissMapper permissMapper;
	
	@Autowired
	private SusrRoleMapper roleMapper;
	
	@Autowired
	private SusrRolePermissMapper rolePermMapper;
	
	@Autowired
	private SusrUserRoleMapper userRoleMapper;
	
	@Autowired
	private IdService idService;

	@Override
	public User001Input createUser(User001Input user) {
	    SusrUser bean = new SusrUser();
	    
	    bean.setId(idService.nextId());
	    bean.setCreateTime(System.currentTimeMillis());
	    bean.setUpdateTime(bean.getCreateTime());
	    bean.setLastLoginTime(bean.getCreateTime());
	    bean.setEmail("");
	    
	    //  设置凭证和密码
	    bean.setTelephone(user.getPrincipals());
	    bean.setSalt(PasswordHelper.generatorSalt());
	    bean.setPassword(PasswordHelper.encryptPassword(user.getCredentials(), bean.getSalt()));
	    
	    //  设置用户
	    Long userId = LocalSessionFactory.getUserId();
	    userId = userId == null ? 0L : userId;
	    bean.setCreateUserId(userId);
	    bean.setUpdateUserId(userId);
	    
	    bean.setUserStatus(user.getStatus());
	    
	    userMapper.insert(bean);
	    
	    user.setUserId(bean.getId());
	    
	    return user;
	}

	@Override
	public void changePassword(Long userId, String newPassword) {
	    SusrUser userBean = userMapper.selectByPrimaryKey(userId);
	    
	    userBean.setSalt(PasswordHelper.generatorSalt());
	    userBean.setPassword(PasswordHelper.encryptPassword(newPassword, userBean.getSalt()));
	    
	    userBean.setUpdateUserId(LocalSessionFactory.getUserId());
	    userBean.setUpdateTime(System.currentTimeMillis());
	    
	    userMapper.updateByPrimaryKey(userBean);
	}

	@Override
	public boolean login(String principals, String credentials) {
	    SusrUser userBean = userMapper.getUserByPhone(principals);
	    
	    if(userBean == null){
	      return false;
	    }
	    
	    //  进行密码的比较
	    return PasswordHelper.veryfyPassword(credentials,userBean.getSalt(),userBean.getPassword());
	}

	@Override
	public boolean logout(Long userId) {
		return true;
	}

	@Override
	public Set<String> findRoles(Long userId) {
		return userRoleMapper.selectRoleNamesByUserId(userId);
	}

	@Override
	public Set<String> findPermissions(Long userId) {
		return rolePermMapper.selectPermissNameByUserId(userId);
	}

	@Override
	public User001Input findUserInfo(Long userId) {
		SusrUser userBean = userMapper.selectByPrimaryKey(userId);
		
		User001Input input = new User001Input();
		
		input.setCredentials(userBean.getPassword());
		input.setPrincipals(userBean.getTelephone());
		input.setSalt(userBean.getSalt());
		input.setStatus(userBean.getUserStatus());
		input.setUserId(userBean.getId());
		
		return input;
	}

	@Override
	public void correlationRoles(Long userId, Long... roleIds) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void uncorrelationRoles(Long userId, Long... roleIds) {
		// TODO Auto-generated method stub
		
	}
	


}
